﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HRProject.Code.DTO;
using System.Collections.Generic;

namespace HRProject.Code.DAO
{
    public class WorktimeRecordDAO : BaseDAO
    {
        public WorktimeRecordDAO()
            : base()
        {
        }

        public BLWorktimeRecord Search(BLWorktimeProperty filter)
        {
            BLWorktimeRecord result = new BLWorktimeRecord(filter.AgentId, filter.StartTime);
            List<BaseCondition> condition = new List<BaseCondition>();
            condition.Add(new BaseCondition("@agentId", filter.AgentId, ParameterDirection.Input));
            condition.Add(new BaseCondition("@date", filter.StartTime, ParameterDirection.Input));
            DataTable data = select("usp_GetWorktimeInDate", condition);
            for (int i = 0; i < data.Rows.Count; i++)
            {
                BLWorktime w = new BLWorktime();
                w.Id = (Guid)data.Rows[i]["WorktimeId"];
                w.AgentId = filter.AgentId;
                w.StartTime = (DateTime)data.Rows[i]["StartTime"];
                w.EndTime = (DateTime)data.Rows[i]["EndTime"];
                BLWorktimeProperty p = new BLWorktimeProperty(filter.AgentId, filter.StartTime);
                p.Id = (Guid)data.Rows[i]["WorktimePropertyId"];
                p.Description = (string)data.Rows[i]["Description"];
                p.Approved = (int)data.Rows[i]["IsApproved"];
                p.UpdateDate = (DateTime)data.Rows[i]["UpdateDate"];
                p.UpdateUserId = (Guid)data.Rows[i]["UpdateUserId"];

                result.FullName = (string)data.Rows[i]["FullName"];
                if (!p.Id.Equals(Guid.Empty))
                {
                    result.Property = p;
                }
                if (!w.Id.Equals(Guid.Empty))
                {
                    result.DailyActivities.Add(w);
                }
            }
            return result;
        }

        public int CountTotalHoursInMonth(Guid agentId, int year, int month)
        {
            int result = 0;
            List<BaseCondition> condition = new List<BaseCondition>();
            condition.Add(new BaseCondition("@agentId", agentId, ParameterDirection.Input));
            condition.Add(new BaseCondition("@year", year, ParameterDirection.Input));
            condition.Add(new BaseCondition("@month", month, ParameterDirection.Input));
            DataTable data = select("usp_CountTotalHoursInMonth", condition);
            if (data.Rows.Count > 0)
            {
                result = (int)data.Rows[0]["Total"];
            }
            return result;
        }
    }
}
